package com.example.shop_2210.frag

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.base.mvvm.BaseFragment
import com.example.base.mvvm.BaseViewFragment
import com.example.shop_2210.R
import com.example.shop_2210.adapter.CateHomeAdapter
import com.example.shop_2210.adapter.GoodsHomeAdapter
import com.example.shop_2210.adapter.MenuHomeAdapter
import com.example.shop_2210.databinding.FragmentHomeBinding
import com.example.shop_2210.fragviewModel.HomeViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader

class HomeFragment : BaseFragment<FragmentHomeBinding,HomeViewModel>() {
    val imglist = mutableListOf<String>()//存放图片网址
    lateinit var menuHomeAdapter: MenuHomeAdapter//延迟初始化菜单适配器
    lateinit var cateHomeAdapter: CateHomeAdapter//延迟初始化分类适配器

    lateinit var goodsHomeAdapter: GoodsHomeAdapter
    var type = 4//因为第一个展示的分类的值为4
    var page = 1
    override fun initData() {
        viewModel.indexSuccess.observe(this){
            if(it.code==200){
                it.data.banner.forEach {
                    imglist.add(it.pic)
                }
                binding.banner.setImages(imglist)
                binding.banner.setImageLoader(object :ImageLoader(){
                    override fun displayImage(
                        context: Context?,
                        path: Any?,
                        imageView: ImageView?
                    ) {
                        if (context != null&&imageView!=null) {
                            Glide.with(context).load(path).into(imageView)
                        }
                       // Glide.with(context!!).load(path).into(imageView!!)
                    }
                })
                binding.banner.setDelayTime(1500)//设置轮播时长
                binding.banner.setBannerStyle(BannerConfig.NUM_INDICATOR)//设置指示器样式
                binding.banner.setOnBannerListener {
                    ToastUtils.showLong("点击了第${it+1}张图片")
                }
                binding.banner.start()

                //显示菜单数据
                menuHomeAdapter.data.clear()//清空原来的菜单数据
                menuHomeAdapter.data.addAll(it.data.menus)//加载新查询出来的数据
                menuHomeAdapter.notifyDataSetChanged()//刷新适配器

                //显示分类数据
                cateHomeAdapter.data.clear()
                cateHomeAdapter.data.addAll(it.data.explosiveMoney)
                cateHomeAdapter.notifyDataSetChanged()

            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.goodsSuccess.observe(this){
            if(it.code==200){
                if(it.data.total>0){
                    //只有页吗为1时，清空数据，其他清空直接添加数据
                    if(page==1){
                        goodsHomeAdapter.data.clear()
                    }
                    goodsHomeAdapter.data.addAll(it.data.list)
                    goodsHomeAdapter.notifyDataSetChanged()
                }else{
                    ToastUtils.showLong("没有加载到更多数据")
                }
            }
        }
    }

    override fun initView() {
        //fragment嵌套时使用
     /*   val navHostFragment = childFragmentManager.findFragmentById(R.id.fragmentContainerView2) as NavHostFragment
        val navController = navHostFragment.navController
        binding.bnvHome.setupWithNavController(navController)*/
        //初始化菜单布局
        menuHomeAdapter = MenuHomeAdapter()
        binding.rvMenu.adapter = menuHomeAdapter
        binding.rvMenu.layoutManager = GridLayoutManager(context,5)
        //初始化分类布局
        cateHomeAdapter = CateHomeAdapter()
        binding.rvCate.adapter = cateHomeAdapter
        binding.rvCate.layoutManager = GridLayoutManager(context,4)
        //请求数据
        viewModel.index()

        //初始化布局
        goodsHomeAdapter = GoodsHomeAdapter()
        binding.rvGoods.adapter = goodsHomeAdapter
        binding.rvGoods.layoutManager = GridLayoutManager(context,2)

        //请求数据
        viewModel.indexGoods(type,5,page)

        //刷新加载更多方法
        binding.srl.setOnRefreshLoadMoreListener(object:OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.indexGoods(type,5,page)
                binding.srl.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.indexGoods(type,5,page)
                binding.srl.finishLoadMore()
            }
        })

        //点击分类，切换不同的数据
        cateHomeAdapter.setOnItemClickListener { adapter, view, position ->

            type = cateHomeAdapter.data[position].type.toInt()
            cateHomeAdapter.setType(type)
            page = 1
            viewModel.indexGoods(type,5,page)
        }

        binding.tvSearch.setOnClickListener {
            ARouter.getInstance().build("/app/SearchActivity").navigation()
        }
        //跳转到详情页
        goodsHomeAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DetailActivity").withInt("goodsId",goodsHomeAdapter.data[position].id).navigation()
        }


    }

    override fun getViewModel(): Class<HomeViewModel>  = HomeViewModel::class.java

    override fun getLayout(): Int = R.layout.fragment_home


}